package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.ProcurementSupplierService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.time.OffsetDateTime

@RequestMapping("procurementSuppliers")
@RestController
class ProcurementSupplierController(
    private val procurementSupplierService: ProcurementSupplierService
) {

    data class QueryProcurementSuppliersRequest(
        val name: String?,
    )

    @HasAnyAuthority(Authority.采购_供货商_查询)
    @GetMapping(params = ["page"])
    fun queryProcurementSuppliers(pageable: Pageable, queryProcurementSuppliersRequest: QueryProcurementSuppliersRequest): Page<QueryProcurementSuppliersResponse> {
        return this.procurementSupplierService.queryProcurementSuppliers(pageable, queryProcurementSuppliersRequest)
    }

    data class QueryProcurementSuppliersResponse(
        val name: String,
        val id: Int,
        val amount:Int,
        val lastOrderSubmittedDateTime:OffsetDateTime?,
    )

    data class UpdateProcurementSupplierRequest(
        val name: String,
    )

    @HasAnyAuthority(Authority.采购_供货商_修改)
    @PutMapping("{id}")
    fun updateProcurementSupplier(@PathVariable id: Int, @RequestBody updateProcurementSupplierRequest: UpdateProcurementSupplierRequest) {
        this.procurementSupplierService.updateProcurementSupplier(id, updateProcurementSupplierRequest)
    }

    @HasAnyAuthority(Authority.采购_供货商_新增)
    @PostMapping
    fun createProcurementSupplier(@RequestBody updateProcurementSupplierRequest: UpdateProcurementSupplierRequest): Int {
        return this.procurementSupplierService.createProcurementSupplier(updateProcurementSupplierRequest)
    }

    @DeleteMapping("{id}")
    fun deleteProcurementSupplier(@PathVariable id: Int) {
        this.procurementSupplierService.deleteProcurementSupplier(id)
    }

    @GetMapping
    fun getAllProcurementSuppliers(): List<IntIdAndName> {
        return this.procurementSupplierService.getAllProcurementSuppliers()
    }

    @HasAnyAuthority(Authority.采购_供货商_查询)
    @GetMapping("{id}")
    fun getProcurementSupplierDetail(@PathVariable id: Int):ProcurementSupplierService.ProcurementSupplierDetail{
        return this.procurementSupplierService.getProcurementSupplierDetail(id)
    }

    @GetMapping(params = ["projection=summary"])
    @HasAnyAuthority(Authority.采购_供货商_查询)
    fun queryProcurementSupplierSummary(keyword:String?,pageable: Pageable):Page<IntIdAndName>{
        return this.procurementSupplierService.queryProcurementSupplierSummary(keyword,pageable)
    }

    /**
     * 查询所有商品包含零售价和最近一次采购的价格
     */
    @GetMapping("{id}/goods", params = ["projection=withHistoryProcurement"])
    fun findAllGoodsWithHistoryProcurement(@PathVariable id: Int):List<ProcurementSupplierService.GoodsSummaryWithRememberedPurchasePriceWithSubGoods>{
        return this.procurementSupplierService.findAllGoodsWithHistoryProcurement(id)
    }

    /**
     * 查询所有商品包含零售价和最近一次采购退货或采购的价格
     */
    @GetMapping("{id}/goods", params = ["projection=withHistoryProcurementReturn"])
    fun findAllGoodsWithHistoryProcurementReturn(@PathVariable id: Int):List<ProcurementSupplierService.GoodsSummaryWithRememberedReturnPriceWithSubGoods>{
        return this.procurementSupplierService.findAllGoodsWithHistoryProcurementReturn(id)
    }

}